package com.gzhryc.shared_device.oem.micro.queue.handlers;

import com.gzhryc.common.event.AbsDelayHandler;
import com.gzhryc.common.exceptions.LogicException;
import com.gzhryc.common.logging.Logger;
import com.gzhryc.pay.enums.ReceivingMoneyType;
import com.gzhryc.shared_device.base.models.OperateInfo;
import com.gzhryc.shared_device.base.enums.EOperatorType;
import com.gzhryc.shared_device.oem.code.user.dao.db.UserTransferMoneyOrder;
import com.gzhryc.shared_device.oem.code.user.dao.enums.UserTransferMoneyOrderEnum;
import com.gzhryc.shared_device.oem.code.user.event.UserAutoTransferMoneyEvent;
import com.gzhryc.shared_device.oem.code.user.services.UserTransferMoneyOrderService;
import com.gzhryc.shared_device.oem.micro.queue.MicroConstants;

/**
 * 自动转账事件处理
 * @version 2023-01-12
 */
public class UserAutoTransferMoneyEventHandler extends AbsDelayHandler<UserAutoTransferMoneyEvent> {

    static Logger log = Logger.getLogger(UserAutoTransferMoneyEventHandler.class);

    @Override
    public boolean execute(UserAutoTransferMoneyEvent event) {
        UserTransferMoneyOrder transferMoneyOrder = UserTransferMoneyOrderService.self().getByTradeNo(event.getTradeNo());
        if(UserTransferMoneyOrderEnum.State.Check.index().equals(transferMoneyOrder.getState())){
            if(ReceivingMoneyType.Account.index().equals(transferMoneyOrder.getReceivingType())){
                OperateInfo operateInfo = event.getOperateInfo();
                if(operateInfo == null){
                    operateInfo = new OperateInfo(MicroConstants.PROGRAM_TYPE, MicroConstants.PROGRAM_TYPE_NAME);
                    operateInfo.setOperator(EOperatorType.System.index(), "自动转账处理程序");
                    operateInfo.setOperateNote("系统自动转账");
                }
                try {
                    UserTransferMoneyOrderService.self().autoProcessing(transferMoneyOrder, operateInfo);
                } catch (LogicException e) {
                    log.error(e.getMessage(),e);
                }
            }
        }
        return true;
    }
}
